package cn.chl.service.impl;

import cn.chl.domain.Build;
import cn.chl.domain.Device;
import cn.chl.domain.Floor;
import cn.chl.dto.DeviceDto;
import cn.chl.dto.DeviceListByBuildIdDto;
import cn.chl.dto.FloorDto;
import cn.chl.feign.DeviceFeignClient;
import cn.chl.feign.FloorFeignClient;
import cn.chl.mapper.BuildMapper;
import cn.chl.result.JSONResult;
import cn.chl.service.IBuildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuildServiceImpl implements IBuildService {

    @Autowired
    private BuildMapper buildMapper;
    @Autowired
    private FloorFeignClient floorFeignClient;
    @Autowired
    private DeviceFeignClient deviceFeignClient;

    /**
     * 查询单个数据
     */
    @Override
    public Build getOne(Integer id) {
        return buildMapper.getOne(id);
    }

    /**
     * 批量添加楼层
     */
    @Override
    public void addFloor(List<Floor> list) {
        for (Floor floor : list) {
            if (floor.getName() == null){
                throw new RuntimeException("楼层名不能为空！");
            }
            if (floor.getBuildId() == null){
                throw new RuntimeException("楼栋不能为空！");
            }
            floorFeignClient.add(floor);
        }
    }

    @Override
    public DeviceListByBuildIdDto getDeviceListByBuildId(Integer id) {
        DeviceListByBuildIdDto dto = new DeviceListByBuildIdDto();

        // 远程调用通过buildID查询出在同一栋楼的device集合
        List<Device> buildByIds = deviceFeignClient.getBuildById(id);
        // 因为都是同一栋楼的device，所以随便拿一个device来获取出楼栋的名字
        Build build = buildMapper.getOne(buildByIds.get(0).getBuildId());
        // 放入返回的对象中
        dto.setBuildName(build.getName());
        // 遍历device集合拿到所有的floorId，这个时候的floorId全部都是同一楼栋的
        List<Integer> floorIds = new ArrayList<>();
        for (Device buildById : buildByIds) {
            floorIds.add(buildById.getFloorId());
        }

        List<FloorDto> floorList = new ArrayList<>();

        // 遍历所有floorId
        for (Integer floorId : floorIds) {
            // 用准备好的floorDto来装同一栋楼不同的楼层，和每个楼层不同的设备
            FloorDto floorDto = new FloorDto();
            // 通过floorId获取到floor的名字，并装入floorDto对象
            Floor floor = floorFeignClient.getOne(floorId);
            floorDto.setName(floor.getName());
            // 通过floorId拿到所有的device，这个时候的device就是同一楼栋的
            List<Device> floorByIds = deviceFeignClient.getFloorById(floorId);
            // 用来装同一栋楼，同一层的设备
            List<DeviceDto> deviceList = new ArrayList<>();
            for (Device floorById : floorByIds) {
                if (floorById.getBuildId() == id){
                    DeviceDto deviceDto = new DeviceDto();
                    // 遍历device装入到deviceDto对象
                    deviceDto.setName(floorById.getName());
                    if (floorById.getType() == 1){
                        deviceDto.setType("烟感");
                    }else if (floorById.getType() == 2){
                        deviceDto.setType("手报");
                    }else if (floorById.getType() == 3){
                        deviceDto.setType("摄像头");
                    }
                    // 再装入deviceList集合
                    deviceList.add(deviceDto);
                }
            }
            // 最后放到floorDto对象
            floorDto.setDeviceList(deviceList);
            floorList.add(floorDto);
        }
        dto.setFloorList(floorList);
        return dto;
    }

    /**
     * 根据设备类型查询所有设备
     */
    @Override
    public List<DeviceListByBuildIdDto> getDeviceListByDeviceType(Integer type) {
        List<DeviceListByBuildIdDto> list = new ArrayList<>();

        // 远程调用通过type查询出所有的device集合
        List<Device> typeById = deviceFeignClient.getTypeById(type);
        for (Device device : typeById) {
            Build build = buildMapper.getOne(device.getBuildId());
            Floor floor = floorFeignClient.getOne(device.getFloorId());

            // 创建存放结果的对象，第一层的
            DeviceListByBuildIdDto buildDto = new DeviceListByBuildIdDto();
            // 获取第一层对象的 buildName
            buildDto.setBuildName(build.getName());

            // 创建list集合来存放结果第二层的对象
            List<FloorDto> floorList = new ArrayList<>();
            FloorDto floorDto = new FloorDto();
            floorDto.setName(floor.getName());
            // 创建list集合来存放结果第三层的对象
            List<DeviceDto> deviceList = new ArrayList<>();
            DeviceDto deviceDto = new DeviceDto();
            deviceDto.setName(device.getName());
            if (device.getType() == 1){
                deviceDto.setType("烟感");
            }else if (device.getType() == 2){
                deviceDto.setType("手报");
            }else if (device.getType() == 3){
                deviceDto.setType("摄像头");
            }
            deviceList.add(deviceDto);
            floorDto.setDeviceList(deviceList);
            floorList.add(floorDto);
            buildDto.setFloorList(floorList);
            list.add(buildDto);
        }
        return list;
    }
}
